﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KX.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System;
    using System.Diagnostics.Contracts;
    using System.Globalization;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The KX class.
    /// </summary>
    public sealed class KX : Record
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="KX"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public KX(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            this.Preference = recordReader.ReadUInt16();
            this.Exchanger = recordReader.ReadDomainName();
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="KX" /> class.
        /// </summary>
        public KX()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Exchanger.
        /// </summary>
        public string Exchanger { get; private set; }

        /// <summary>
        ///     Gets Preference.
        /// </summary>
        public ushort Preference { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Compares the specified left.
        /// </summary>
        /// <param name="left">
        /// The left.
        /// </param>
        /// <param name="right">
        /// The right.
        /// </param>
        /// <returns>
        /// The compare to.
        /// </returns>
        public static int Compare(KX left, KX right)
        {
            if (ReferenceEquals(left, right))
            {
                return 0;
            }

            if (ReferenceEquals(left, null))
            {
                return -1;
            }

            return left.CompareTo(right);
        }

        /// <summary>
        /// Compares to.
        /// </summary>
        /// <param name="obj">
        /// The obj A.
        /// </param>
        /// <returns>
        /// The compare to.
        /// </returns>
        public int CompareTo(object obj)
        {
            if (null == obj)
            {
                return 1;
            }

            var record = obj as KX;

            if (null == record)
            {
                throw new ArgumentException("A RecordKx object is required for comparison.");
            }

            return this.CompareTo(record);
        }

        /// <summary>
        /// Compares to.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// The compare to.
        /// </returns>
        public int CompareTo(KX other)
        {
            if (ReferenceEquals(other, null))
            {
                return 1;
            }

            if (this.Preference > other.Preference)
            {
                return 1;
            }

            if (this.Preference < other.Preference)
            {
                return -1;
            }

            // they are the same, now compare case insensitive names
            return string.Compare(this.Exchanger, other.Exchanger, StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// The equals.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The equals.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }

            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            if (obj.GetType() != typeof(KX))
            {
                return false;
            }

            return this.Equals((KX)obj);
        }

        /// <summary>
        /// The equals.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// The equals.
        /// </returns>
        public bool Equals(KX other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return Equals(other.Exchanger, this.Exchanger) && other.Preference == this.Preference;
        }

        /// <summary>
        ///     Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        ///     A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
        /// </returns>
        public override int GetHashCode()
        {
            unchecked
            {
                return ((this.Exchanger != null ? this.Exchanger.GetHashCode() : 0) * 397)
                       ^ this.Preference.GetHashCode();
            }
        }

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{0} {1}", this.Preference, this.Exchanger);
        }

        #endregion
    }
}